<?php
/**
*
* 功能描述（配送数据）
*
* @author suoya <jlusuoya@gmail.com>
* @version 1
*/

class ModelDely extends ModelBase
{
	function GetList($page, $status, $d_start, $d_end, $key, $keyword)
	{
		$where = "1";
		if($status != 0)
			$where .= " and t2.status = $status";
		if(!empty($key) && !empty($keyword))
			$where .= " and $key like '%$keyword%'";
		if($d_start != '')
			$where .= " and delivery_time >= '$d_start'";
		if($d_end != '')
			$where .= " and delivery_time <= '$d_end'";
		$sql = "select count(1) cnt  from `dely_info` t2 left join `order_base` t1 on t1.id = t2.id where $where";
		$data = $this->db->getRowBySql($sql, 'Row');
		if($data['cnt'] == 0)
			return array('cnt' => 0, 'pageCnt' => 0, 'data' => array());

		$retData = array('cnt' => $data['cnt'], 'pageCnt' => ceil($data['cnt'] / ADM_PAGESIZE));
		$limit = ADM_PAGESIZE * ($page - 1) . ", " . ADM_PAGESIZE;
		$sql = "select t2.id, dely_adm, delivery_time, consignee, mobile_to, area, address, userid, mobile_from, t2.status, note, starttime, finishtime, amount, message from `dely_info` t2 left join `order_base` t1 on t1.id = t2.id where $where limit $limit";
		$data = $this->db->getRowBySql($sql, 'All');
		$retData['data'] = $data;
		return $retData;
	}

	function UpdateOrder($data, $id)
	{
		$cnt = $this->db->Update("order_base", $data, "id = $id");
		return $cnt;
	}

	function GetAllInfo($id)
	{
		$fild1 = 't1.`id`, `type`, `userid`, t1.`status`, `add_adm`, `cofm_adm`, `createtime`, `pay_type`, `pay_adm`, `trade_money1`, `trade_money2`, `money1_code`, `money2_code`, `add_score`, `amount_pre`, `amount`, `card1`, `card2`, `attach1`, `attach2`, `area`, `address`, `consignee`, `mobile_to`, `mobile_from`, `sign_building`, `zipcode`, `delivery_type`, `delivery_time`, `inv_type`, `inv_title`, `message`';
		$fild2 = 't2.`id` delyid, t2.`status` delystatus, `dely_adm`, `starttime`, `finishtime`, `note`';
		$sql = "select $fild1, $fild2 from `dely_info` t2 left join `order_base` t1 on t1.id = t2.id where t2.id = $id";
		$data = $this->db->getRowBySql($sql, 'Row');
		return $data;
	}

	function GetAllGoods($id)
	{
		$fild1 = 't1.`id`, `order_id`, `good_id`, `name`, `version`, `size`, `price`, `cnt`, `discount`, `comment_id`';
		$fild2 = 't2.`id` prod_id, `item_id`, `status`, `prod_adm`, `timelimit`, `finishtime`, `taketime`, `takeman`, `note`';
		$sql = "select $fild1, $fild2 FROM `prod_info` t2 left join `order_goods` t1 on t1.id = t2.item_id where order_id = $id order by order_id desc";
		$data = $this->db->getRowBySql($sql, 'All');
		return $data;
	}
}

